<template>
  <div class="szLogin sz-login__openZyk">
    <div class="header">申请开通资源库</div>
    <div class="content">
      <el-form ref="applyFormRef" :model="applyForm" :rules="applyRules">
        <el-form-item prop="school">
          <el-input v-model="applyForm.school" placeholder="请输入学校" />
        </el-form-item>
        <el-form-item prop="proposer">
          <el-input v-model="applyForm.proposer" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item prop="department">
          <el-input
            v-model="applyForm.department"
            placeholder="请输入所在部门"
          />
        </el-form-item>
        <el-form-item prop="position">
          <el-input v-model="applyForm.position" placeholder="请输入职位" />
        </el-form-item>
        <el-form-item prop="phone" class="phoneInput">
          <el-input
            name="phone"
            v-model="applyForm.phone"
            placeholder="请输入手机号"
          >
            <template slot="prefix">+86</template>
          </el-input>
        </el-form-item>
        <el-form-item prop="captcha" class="captchaGroup">
          <el-input
            name="captcha"
            v-model="applyForm.captcha"
            placeholder="请输入验证码"
          >
          </el-input>
          <el-button
            class="captchaButton"
            :disabled="hasSendSms"
            @click="handleGetImgCaptcha"
            >{{
              hasSendSms ? `（${countdown}）重新获取` : "获取验证码"
            }}</el-button
          >
        </el-form-item>
      </el-form>
      <div class="footer">
        <el-button class="confirm" type="primary" @click="handleSubmit"
          >提交申请信息</el-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import { sendRegisterSms } from "@/apis/login";
import { phoneReg } from '@/utils/regexp'
let countdownId = null;
export default {
  name: "OpenZyk",
  data() {
    const checkPhoneNumber = (rule, value, callback) => {
      if (value.length === 0) callback(new Error("请输入手机号"));
      if (!phoneReg.test(value)) callback(new Error("请输入正确的手机号"));
      if (value.length < 11 && value.length > 0) callback(new Error("手机号不足11位"));
      callback();
    };
    return {
      applyForm: {
        phone: "",
        school: "",
        proposer: "",
        department: "",
        position: "",
        captcha: ""
      },
      applyRules: {
        school: [
          { required: true, message: "请输入学校名称", trigger: "blur" }
        ],
        proposer: [
          { required: true, message: "请输入申请人", trigger: "blur" }
        ],
        position: [{ required: true, message: "请输入职位", trigger: "blur" }],
        department: [
          { required: true, message: "请输入所在部门", trigger: "blur" }
        ],
        phone: [
          { required: true, validator: checkPhoneNumber, trigger: "blur" }
        ],
        captcha: [{ required: true, message: "请输入验证码", trigger: "blur" }]
      },
      hasSendSms: false,
      countdown: 60
    };
  },
  watch: {
    imgCaptchaInfo(newVal, oldVal) {
      if (newVal && newVal.captcha) {
        this.handleCheckImgCaptcha(newVal);
      }
    }
  },
  methods: {
    handleGetImgCaptcha() {
      this.$refs["applyFormRef"].validateField("phone", error => {
        if (!error) {
          this.$emit("openDialog");
        }
      });
    },
    async handleCheckImgCaptcha({ captcha, verifyKey }) {
      try {
        const params = {
          captcha,
          cellphone: this.applyForm.phone,
          regionCode: "86",
          verKey: verifyKey
        };
        const res = await sendRegisterSms(params);
        if (res.status === 1) {
          this.$message.success("验证码已发送");
          this.$emit("closeDialog");

          // 禁用获取验证码按钮
          this.hasSendSms = true;
          countdownId = setInterval(() => {
            if (this.countdown > 0) {
              this.countdown -= 1;
            } else {
              clearInterval(countdownId);
              this.hasSendSms = false;
              this.countdown = 60;
            }
          }, 1000);
        } else {
          this.$message({ type: 'error', message: res.message });
        }
      } catch (err) {
        console.error(err);
      }
    },
    handleSubmit() {
      this.$refs["applyFormRef"].validate((valid) => {
        if (valid) {
          console.log('验证成功')
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
@import url("/styles/login.scss");
.sz-login__openZyk {
  .header {
    color: #303133;
    text-align: center;
    font-size: 20px;
    font-weight: 600;
    margin-bottom: 30px;
  }
  .content {
    .password {
      position: relative;
    }
    .passwordRule {
      font-weight: 400;
      font-size: 10px;
      line-height: 12px;
      color: #c0c4cc;
    }
  }
  .footer {
    text-align: center;
    .confirm {
      width: 100%;
      margin-top: 10px;
    }
    .change {
      padding: 0;
      line-height: 12px;
      margin-top: 15px;
    }
  }
}
</style>
